<?php  
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class GoodsController extends AdminBaseController{
	public function index(){
		$page = I('get.page/d',1);
		$title = I('get.title/s','');
		$wheresql = empty($title) ? '' : 'title like "%'. $title .'%" or signum like "%'.$title.'%" or content like "%'.$title.'%"';
		$num = 5;
		$totalnum = M('Goods') -> where($wheresql) -> count();
		$pagenum  = ceil($totalnum/$num);
		$frow = empty($page - 1) ? 0 : (($page - 1) * $num);	
		$goods = D('goods');
		$list = $goods -> goods_list(0,'g.id,g.cat_id,g.title,g.signum,g.img,g.keywords,g.description,g.content,g.addtime,g.addman,g.hits,g.sort,c.coltitle','g','left join dx_columns c on g.cat_id = c.id',$wheresql,$frow,$num);
		$this -> assign('list',$list['data']);
		$this -> assign('title',$title);
		$this -> assign('page', $pagenum);
		$this -> display();
	}
	public function add(){
		$goods = M('Columns');
		$list = $goods -> field('id,coltitle') -> where(['type' => 2]) -> select();
		$this -> assign('opt',$list);
		$this -> display();
	}

	public function save(){
		if(IS_POST || IS_AJAX){
			$goods = D('goods');
			$result = $goods -> addGood(I('post.'));
			$this -> ajaxReturn($result);
		}
	}

	public function edit(){
		if(IS_GET){
			$goods = M('Columns');
			$list = $goods -> field('id,coltitle') -> where(['type' => 2]) -> select();
			$result = D('goods') -> goods_list(I('get.id/d',0),'*');//三维数组
			$this -> assign('opt',$list);
			$this -> assign('result',$result['data'][0]);
			$this -> display();
		}

	}

	public function del(){
		if(IS_POST){
			$result = D('goods') -> Delgoods(I('post.id/d',0));
			$this -> ajaxReturn($result);
		}
	}

	public function upload(){
		//多图上传展示通过id
		if(IS_GET){
			$id = I('get.id/d',0);
			$result = M('Goods') -> field('img') -> where(['id' => $id]) -> find();
			$img = implode(',', $result);
			$imgarr = explode(',', rtrim($img,','));   
			$this -> assign('list',$imgarr);
			$this -> display();
		}
		
	}




}


